home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / ShareMailGiftware / AmigaTalk / general / ArrayedCollection.st < prev    next >
Text File  |  2002-10-27  |  4KB  |  140 lines

  1. " --------------------------------------------------------------- "
  2. " Added the with: methods on 04-Apr-2002.                         "
  3. " --------------------------------------------------------------- "
  4.  
  5. Class ArrayedCollection :SequenceableCollection
  6. ! current !
  7. [
  8.     = anArray         ! i !
  9.       (self size ~= anArray size) 
  10.          ifTrue: [^ false]. 
  11.             
  12.       i <- 0.
  13.  
  14.       self do: [:x | (x ~= (anArray at: (i <- i + 1)))
  15.                                         ifTrue: [^ false]
  16.                ].
  17.       ^ true
  18. |
  19.     at: key ifAbsent: exceptionBlock
  20.       ((key <= 0) or: [key > self size])
  21.          ifTrue: [^ exceptionBlock value].
  22.  
  23.       ^ self at: key
  24. |
  25.     coerce: aCollection      ! temp !
  26.       temp <- self class new: aCollection size.
  27.  
  28.       temp replaceFrom: 1 to: aCollection size with: aCollection.
  29.  
  30.       ^ temp
  31. |
  32.     copyFrom: start to: stop       ! size temp !
  33.       size <- stop - start + 1.
  34.       temp <- self class new: size.
  35.       temp replaceFrom: 1 to: size with: self startingAt: start.
  36.       ^ temp
  37. |
  38.     currentKey
  39.       ^ current
  40.     deepCopy ! newobj !
  41.       newobj <- self class new: self size.
  42.  
  43.       (1 to: self size) do: [:i | newobj at: i put: (self at: i) copy ].
  44.  
  45.       ^ newobj
  46. |
  47.     do: aBlock
  48.       (1 to: self size) 
  49.           do: [:i | current <- i. 
  50.             aBlock value: (self at: i)]
  51. |
  52.     first
  53.       current <- 1.
  54.       ^ (current <= self size) 
  55.          ifTrue: [ self at: current]
  56. |
  57.     firstKey
  58.       ^ 1
  59. |
  60.     lastKey
  61.       ^ self size
  62. |
  63.     next
  64.       current <- current + 1.
  65.       ^ (current <= self size) 
  66.          ifTrue: [ self at: current]
  67. |
  68.     padTo: length
  69.       ^ (self size < length)
  70.           ifTrue: [ self , (self class new: (length - self size) ) ]
  71.          ifFalse: [ self ]
  72. |
  73.     shallowCopy ! newobj !
  74.       newobj <- self class new: self size.
  75.  
  76.       (1 to: self size) do: [:i | newobj at: i put: (self at: i) ].
  77.  
  78.       ^ newobj
  79. |
  80.     with: anObject ! newCollection !
  81.       " Answer a new instance of ArrayedCollection, containing only anObject."   
  82.          
  83.       newCollection <- self new: 1.   
  84.  
  85.       newCollection at: 1 put: anObject.   
  86.  
  87.       ^ newCollection 
  88.     with: firstObject with: secondObject ! newCollection !
  89.       " Answer a new instance of ArrayedCollection, containing firstObject
  90.       * and secondObject.
  91.       "
  92.       newCollection <- self new: 2.
  93.  
  94.       newCollection at: 1 put: firstObject.   
  95.       newCollection at: 2 put: secondObject.   
  96.    
  97.       ^ newCollection 
  98.     with: firstObject with: secondObject with: thirdObject ! newCollection !
  99.       " Answer a new instance of ArrayedCollection, 
  100.       * containing only these three objects.
  101.       "   
  102.       newCollection <- self new: 3.   
  103.      
  104.       newCollection at: 1 put: firstObject.   
  105.       newCollection at: 2 put: secondObject.   
  106.       newCollection at: 3 put: thirdObject.   
  107.      
  108.       ^ newCollection 
  109.     with: firstObject with: secondObject with: thirdObject with: fourthObject    
  110.       ! newCollection !
  111.      
  112.       " Answer a new instance of ArrayedCollection, containing the four
  113.       * arguments as the elements.
  114.       "   
  115.       newCollection <- self new: 4.   
  116.      
  117.       newCollection at: 1 put: firstObject.   
  118.       newCollection at: 2 put: secondObject.   
  119.       newCollection at: 3 put: thirdObject.   
  120.       newCollection at: 4 put: fourthObject.   
  121.      
  122.       ^ newCollection 
  123. |     
  124.     withAll: aCollection ! newCollection index !
  125.       " Answer a new instance of this class, whose elements are 
  126.       * the elements of aCollection. 
  127.       "   
  128.       newCollection <- self new: aCollection size.   
  129.       index         <- 0.   
  130.      
  131.       aCollection do: [:element | newCollection at: (index := index + 1)
  132.                                                put: element ].   
  133.      
  134.       ^ newCollection 
  135. ]
  136.